<?php
if (! defined ( 'BASEPATH' )) {
	exit ( 'no direct script access allowed!' );
}
/**
 * 好友推荐模块操作
 * Enter description here ...
 * @author 庆安
 *
 */
class user_dynamic_model extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	/**
	 * 添加 好友推荐 
	 * Enter description here ...
	 * @param  $data Array
	 * 返回  int 类型 0 添加失败 大于0 成功 
	 */
	function inserUserDynamic($data) {
		$data['userId']=$this->session->userdata("userId");
		return $this->db->insert ( 'wanwan_enum_userdynamic', $data );
		return $this->db->insert_id ();
	}
	/**
	 * 获得用户的动态
	 * Enter description here ...
	 * @param Array $data   条件集合
	 * @param int $beginNumber  开始页码
	 * @param int $pageSize 每页显示条数
	 */
	function queryUserDynamicData($userId,$type,$beginNumber,$pageSize,$id=0){
		$beginNumber--;
		$selectById=($id==0)?"":"and a.id=$id";
		$sql="SELECT a.id,a.title,a.path,a.createTime,a.type,a.userId,a.replyCount,a.forwardingCount,a.imgSrc,a.content,a.description,b.userIcon,b.nickName 
				FROM wanwan_enum_userdynamic a LEFT JOIN wanwan_enum_user b ON a.userId=b.userid WHERE   ('$type'='dynamic' OR a.type='$type') and a.userId=$userId and a.deleFlag=0 $selectById ORDER BY createTime DESC LIMIT $beginNumber,$pageSize";
		log_message("info","sql select con is $sql");
		$rs=$this->db->query($sql);
		return $rs->result_array ();
	}
	/**
	 * 更新好友推荐信息
	 * Enter description here ...
	 * @param  $data Array
	 * 返回 int 类型 0 更新成失败 大于 0执行成功
	 */
	
	function addUserDynamic($data) {
		
		log_message ( "info", json_encode ( $data ) );
		$errorMsg = "个人动态添加成功";
		$successMsg = "";
		$data ['createTime'] = date ( 'Y-m-d H:i:s' );
		$int = $this->inserUserDynamic ( $data );
		if (empty ( $int )) {
			$successMsg = "个人动态添加失败";
		}
		return array ('msg' => $successMsg . $errorMsg . "." );
	}
	
	function queryFriendsList($data) {
		$userId = $data ['userId'];
		$datas=array();
		$sql = "select teamName,teamIcon,teamId from wanwan_enum_team where teamId in(select teamId from wanwan_enum_usertoteam where userId=$userId )";
		$result = $this->db->query ( $sql );
		foreach ( $result->result_array () as $row ) {
			$team = array ();
			$team ['label'] = empty ( $row ['teamName'] ) ? '' : $row ['teamName'];
			$team ['id'] = empty ( $row ['teamId'] ) ? '' : $row ["teamId"];
			$team ['icon'] = empty ( $row ['teamIcon'] ) ? base_url () . 'assets/images/bmlf_04.gif' : base_url () . $row ['teamIcon'];
			$team ['items'] = $this->queryTeamList ( $row ['teamId'] );
			array_push ( $datas, $team );
		}
		return $datas;
	}
	function queryTeamList($teamId) {
		$sql = "SELECT ud.*,u.nickName,u.userBigIcon FROM  wanwan_enum_userdynamic ud LEFT JOIN wanwan_enum_user u ON ud.userId=u.userId 
 				 WHERE ud.userId IN(SELECT userId FROM wanwan_enum_usertoteam WHERE teamId=$teamId) and ud.createTime BETWEEN DATE_ADD(CURDATE(), INTERVAL -30 DAY) AND CURDATE() ORDER BY ud.createTime desc";
		$result = $this->db->query ( $sql );
		log_message("info","sql  ==>".$sql);
		return $result->result_array();
	}
}